Skip to content

Fix line numbers of code fences in docstrings - #75

Merged
Elias Freider (freider) merged 1 commit into
modal-labs:mainfrom
RaphaelFakhri:fix-docstring-line-offsets
Nov 4, 2026
Merged

Elias Freider (freider) merged 1 commit into
modal-labs:mainfrom
RaphaelFakhri:fix-docstring-line-offsets

Conversation

@RaphaelFakhri

Copy link
Copy Markdown

Fixes the docstring line offset used for test IDs and tracebacks of code fences in Python docstrings.

get_docstring_start_line assumed that the opening quotes are on their own line, which inspect.getdoc strips as a leading blank line. The offset was wrong in these cases:

  • Summary on the opening line ("""Summary.): line numbers pointed one line after the actual line.
  • Module docstrings: inspect.getsourcelines returns 0 as the start line of a module, so line numbers pointed one line before the actual line.
  • String prefixes (r"""): the search skipped the opening quotes and matched the closing quotes. When the closing quotes share a line with other text, the docstring was not collected at all.

The offset now counts the leading blank lines that inspect.getdoc strips, accepts r/u prefixes, and treats a module start line as 1. Docstrings with the opening quotes on their own line keep the same line numbers.

Issue: #74

The docstring line offset assumed that the opening quotes are on their
own line. Fences in docstrings with a summary on the opening line
reported the line after the actual one, and fences in module docstrings
reported the line before it. Docstrings with a string prefix (r""") were
matched against their closing quotes, or not collected at all when the
closing quotes share a line with other text.
@freider

Copy link
Copy Markdown
Collaborator

Makes sense, thanks for the fix!

@freider Elias Freider (freider) left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm!

@freider
Elias Freider (freider) merged commit 5ec25e6 into modal-labs:main Nov 4, 2026
5 checks passed
Elias Freider (freider) added a commit that referenced this pull request Nov 5, 2026
#79 switched docstring collection from inspect.getmembers() (alphabetical)
to walking __dict__ (definition order), and #75 landed concurrently with a
consecutive-lines assertion written against the old alphabetical order.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants